package vcp.gui.forms;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import vcp.gui.components.VCPPanel;
import vcp.gui.forms.validators.DateValidator;
import vcp.gui.forms.validators.EmailValidator;
import vcp.gui.forms.validators.NameValidator;
import vcp.gui.forms.validators.NumericValidator;
import vcp.gui.forms.validators.Time24HoursValidator;

import java.sql.Date;

import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import control.OrderController;
import control.SimpleClient;
import control.customTimestamp;
import db.parking.beans.Orders;

import java.awt.Color;
import java.awt.GridLayout;
import java.sql.Timestamp;



import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
import com.toedter.calendar.JDateChooser;

public class OrderTemporaryForm extends VCPPanel implements ActionListener {
	private JTextField textCID;
	private JLabel lblNewOrderError;
	private SimpleClient client;
 
    private int OrderType;
	
	/* validators */
	private boolean isValidForm=false;
	private JTextField carNo_field;
	private JTextField hTimeLeave_field;
	private JTextField email_field;
	private JLabel lblLeaveDate;
	JDateChooser dateChooser;
	JDateChooser dateChooser2;
	private JLabel dotLabel2;
	private JTextField mTimeLeave_field;
	private JTextField hTimeStart_field;
	private JTextField mTimeStart_field;
	private JLabel lblLotNum;
	private JTextField lotNumField;

	public OrderTemporaryForm(SimpleClient client) {
		  buildForm();
		  this.client = client;
		  
	}
	
	private void buildForm(){

		textCID = new JTextField();
		textCID.setColumns(10);

		JLabel lblCID = new JLabel("Customer ID");

		JButton btnSend = new JButton("create");
		btnSend.addActionListener(this);
		btnSend.setActionCommand("btnSend");

		lblNewOrderError = new JLabel("");
		lblNewOrderError.setForeground(Color.RED);
		
		carNo_field = new JTextField();
		carNo_field.setColumns(10);
		
		hTimeLeave_field = new JTextField();
		hTimeLeave_field.setColumns(10);
		
		email_field = new JTextField();
		email_field.setColumns(10);
		
		JLabel lblCarNumber = new JLabel("Car Number");
		
		JLabel lblLeaveTime = new JLabel("Leave Time");
		
		JLabel lblEmail = new JLabel("E-Mail");
		
		lblLeaveDate = new JLabel("Leave Date");
		
		dateChooser = new JDateChooser();
		
		dotLabel2 = new JLabel(":");
		
		mTimeLeave_field = new JTextField();
		mTimeLeave_field.setColumns(10);
		
		dateChooser2 = new JDateChooser();
		
		JLabel lblStartDate = new JLabel("Start Date");
		
		JLabel lblStartTime = new JLabel("Start Time");
		
		hTimeStart_field = new JTextField();
		hTimeStart_field.setColumns(10);
		
		JLabel dotLabel1 = new JLabel(":");
		
		mTimeStart_field = new JTextField();
		mTimeStart_field.setColumns(10);
		
		lblLotNum = new JLabel("Lot Number");
		
		lotNumField = new JTextField();
		lotNumField.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblNewOrderError)
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblCID)
											.addGap(41)
											.addComponent(textCID, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE))
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblLeaveDate, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
											.addGap(18)
											.addComponent(dateChooser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addComponent(carNo_field, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblStartTime, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(hTimeStart_field, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(dotLabel1, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(mTimeStart_field, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
											.addComponent(lblStartDate, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
											.addGap(18)
											.addComponent(dateChooser2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addComponent(lotNumField, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(39, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblLeaveTime, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(hTimeLeave_field, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(dotLabel2)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(mTimeLeave_field, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(285, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblCarNumber, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(365, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblEmail, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(email_field, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(lblLotNum, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
								.addGap(66)))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnSend)
							.addGap(182))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewOrderError)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblCID)
								.addComponent(textCID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCarNumber)
						.addComponent(carNo_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(dateChooser2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(14))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblStartDate)
								.addComponent(lblStartTime)
								.addComponent(hTimeStart_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(dotLabel1)
								.addComponent(mTimeStart_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(18)))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblLeaveTime)
							.addComponent(hTimeLeave_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(dotLabel2)
							.addComponent(mTimeLeave_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblLeaveDate))
						.addComponent(dateChooser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEmail)
						.addComponent(email_field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblLotNum)
						.addComponent(lotNumField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnSend)
					.addContainerGap(8, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		Orders bean = new Orders();
		OrderController Orders;
		//Add input Validators
			NumericValidator verifyID = new NumericValidator();
			Time24HoursValidator verifyTime = new Time24HoursValidator();
			DateValidator verifyDate = new DateValidator();
			EmailValidator verifyEmail = new EmailValidator();
			
			 //Validate that the input is NOT empty
			if(textCID.getText().isEmpty() || carNo_field.getText().isEmpty() || lotNumField.getText().isEmpty() || email_field.getText().isEmpty() || hTimeLeave_field.getText().isEmpty()
				||mTimeLeave_field.getText().isEmpty() || hTimeStart_field.getText().isEmpty() || mTimeStart_field.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null,"Please fill in all fields","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
				return;
			}
		    //Parse all data	
			String ID = textCID.getText();
			String carNo = carNo_field.getText();
			String email = email_field.getText();
			int lot = Integer.parseInt(lotNumField.getText());
			int leaveHours1 = Integer.parseInt(hTimeLeave_field.getText());
			int leaveMinutes1 = Integer.parseInt(mTimeLeave_field.getText());
			String time1 = hTimeLeave_field.getText() +":"+mTimeLeave_field.getText();
			int startHours2 = Integer.parseInt(hTimeStart_field.getText());
			int startMinutes2 = Integer.parseInt(mTimeStart_field.getText());
			String time2 = hTimeStart_field.getText() +":"+mTimeStart_field.getText();
			
			int day1=dateChooser.getJCalendar().getDayChooser().getDay();
			int month1=dateChooser.getJCalendar().getMonthChooser().getMonth()+1;
			int year1=dateChooser.getJCalendar().getYearChooser().getYear();
			String date1 = Integer.toString(day1)+"/"+ Integer.toString(month1) +"/"+ Integer.toString(year1);
			int day2=dateChooser.getJCalendar().getDayChooser().getDay();
			int month2=dateChooser.getJCalendar().getMonthChooser().getMonth()+1;
			int year2=dateChooser.getJCalendar().getYearChooser().getYear();
			String date2 = Integer.toString(day2)+"/"+ Integer.toString(month2) +"/"+ Integer.toString(year2);
			String dateFormat = "dd/MM/yyyy";



			
			//Input Validity Check
			if (ID.equals("") || carNo.equals("") || lot == 0 || !verifyID.validate(ID) || !verifyID.validate(ID)) {
				JOptionPane.showMessageDialog(null,"Invalid ID Entered,Please Re-Enter","Invalid Input Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
			}
			else if (time1.equals("") || time2.equals("") || !verifyTime.validate(time1) || !verifyTime.validate(time2) ) {
					JOptionPane.showMessageDialog(null,"Invalid Time Entered,Please Re-Enter","Invalid Time Error", JOptionPane.ERROR_MESSAGE);
					isValidForm = false;
			}
			else if (date1.equals("") || date2.equals("") || !verifyDate.isThisDateValid(date1, dateFormat) || !verifyDate.isThisDateValid(date2, dateFormat)) {
				JOptionPane.showMessageDialog(null,"Invalid Date Entered,Please Re-Enter","Invalid Date Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
		   }
		   else if (email.equals("") || !verifyEmail.validate(email)) {
				JOptionPane.showMessageDialog(null,"Invalid Email Entered,Please Re-Enter","Invalid Email Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
		   }
		   else if (email.equals("") || !verifyEmail.validate(email)) {
				JOptionPane.showMessageDialog(null,"Invalid Email Entered,Please Re-Enter","Invalid Email Error", JOptionPane.ERROR_MESSAGE);
				isValidForm = false;
		   }
		   else{
				isValidForm = true;
			}
			
			if(isValidForm==true){
				 try {
					Orders = new OrderController(client);
					Timestamp startTimeStamp=customTimestamp.makeTimestamp(year1,month1,day1,Integer.parseInt(hTimeStart_field.getText()),Integer.parseInt(mTimeStart_field.getText()),02,02);
					Timestamp endTimeStamp=customTimestamp.makeTimestamp(year2,month2,day2,Integer.parseInt(hTimeLeave_field.getText()),Integer.parseInt(mTimeLeave_field.getText()),02,02);
					//Check that startDate Timestamp is before leaveDate Timestamp
					if (startTimeStamp.compareTo(endTimeStamp) < 0) {
						JOptionPane.showMessageDialog(null,"Invalid Dates,Please Re-Enter","Invalid Date Error", JOptionPane.ERROR_MESSAGE);
						isValidForm = false;
						return;
				   }
			
					bean.setUid(ID);
					bean.setType("Temporary");
					bean.setCarNo(carNo);
					bean.setStartTime(startTimeStamp);
					bean.setLeaveTime(endTimeStamp);
					bean.setLotNo(Integer.parseInt(lotNumField.getText()));
					bean.setEmail(email);
					bean.setStatus(0); //TODO:what is the status of order when its been only set?
					Orders.InsertTemporary(bean);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
			
				}
			}
	}

}
			
			
		